package pers.lan.stack;

import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;

/**
 * @author <p><a href="mailto:1728209643@qq.com">XiaoSan</a>
 * @date 2019/9/2 3:34 PM
 */
public class LC_71 {

    public static void main(String[] args) {
        System.out.println(
                new LC_71().simplifyPath2("/a//b////c/d//././/..")
        );

    }
    public String simplifyPath2(String path) {
        return Paths.get(path).normalize().toString();
    }


        public String simplifyPath(String path) {
        String[] split = path.split("/");
        List<String> list = new LinkedList<>();
        for (String s : split) {
            switch (s) {
                case "":
                case ".":
                    break;
                case "..":
                    if (!list.isEmpty()) {
                        list.remove(list.size() - 1);
                    }
                    break;
                default:
                    list.add(s);
                    break;
            }
        }
        if (list.isEmpty()) {
            return "/";
        }
        StringBuilder sb = new StringBuilder();
        for (String s : list) {
            sb.append("/")
                    .append(s);
        }

        return sb.toString();
    }
}
